package com.hiultra.archive.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hiultra.archive.domain.vo.ResultEntity;
import com.hiultra.common.exception.ServiceException;
import com.hiultra.common.utils.DateUtils;
import com.hiultra.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hiultra.archive.mapper.RoomArchiveMapper;
import com.hiultra.archive.domain.RoomArchive;
import com.hiultra.archive.service.IRoomArchiveService;

/**
 * 盒中档案信息Service业务层处理
 *
 * @author 派大星
 * @date 2024-07-23
 */
@Service
public class RoomArchiveServiceImpl implements IRoomArchiveService {
    @Autowired
    private RoomArchiveMapper roomArchiveMapper;

    /**
     * 查询盒中档案信息
     *
     * @param id 盒中档案信息主键
     * @return 盒中档案信息
     */
    @Override
    public RoomArchive selectRoomArchiveById(String id) {
        return roomArchiveMapper.selectRoomArchiveById(id);
    }

    /**
     * 查询盒中档案信息列表
     *
     * @param roomArchive 盒中档案信息
     * @return 盒中档案信息
     */
    @Override
    public List<RoomArchive> selectRoomArchiveList(RoomArchive roomArchive) {
        return roomArchiveMapper.selectRoomArchiveList(roomArchive);
    }

    /**
     * 新增盒中档案信息
     *
     * @param roomArchive 盒中档案信息
     * @return 结果
     */
    @Override
    public int insertRoomArchive(RoomArchive roomArchive) {
        //资料名查重

        if (ObjectUtil.isNull(roomArchive.getId())) {
            String id = UUID.randomUUID().toString().replace("-", "");
            roomArchive.setId(id);
        }
        RoomArchive roomArchiveByName = roomArchiveMapper.selectRoomArchiveByName(roomArchive.getArchiveName());
        if (StringUtils.isNull(roomArchiveByName)) {
            roomArchive.setCreateTime(DateUtils.getNowDate());
            return roomArchiveMapper.insertRoomArchive(roomArchive);
        } else {
            throw new ServiceException("档案名称重复,请修改后重试");
        }
    }

    /**
     * 修改盒中档案信息
     *
     * @param roomArchive 盒中档案信息
     * @return 结果
     */
    @Override
    public int updateRoomArchive(RoomArchive roomArchive) {
        //资料名查重
        // RoomArchive roomArchiveByName = roomArchiveMapper.selectRoomArchiveByName(roomArchive.getArchiveName());
//        if (StringUtils.isNull(roomArchiveByName) || roomArchiveByName.getArchiveName().equals(roomArchive.getArchiveName())) {
//            roomArchive.setCreateTime(DateUtils.getNowDate());
        return roomArchiveMapper.updateRoomArchive(roomArchive);
//        } else {
//            throw new ServiceException("档案名称重复,请修改后重试");
//        }
    }

    /**
     * 批量删除盒中档案信息
     *
     * @param ids 需要删除的盒中档案信息主键
     * @return 结果
     */
    @Override
    public int deleteRoomArchiveByIds(String[] ids) {
        return roomArchiveMapper.deleteRoomArchiveByIds(ids);
    }

    /**
     * 删除盒中档案信息信息
     *
     * @param id 盒中档案信息主键
     * @return 结果
     */
    @Override
    public int deleteRoomArchiveById(String id) {
        return roomArchiveMapper.deleteRoomArchiveById(id);
    }

    /**
     * 批量插入房间归档
     *
     * @param roomArchiveList 房间归档对象列表，包含了所有需要批量插入的房间归档数据
     * @return 插入的数据行数，以便调用者可以了解插入操作的结果
     */
    @Override
    public int batchInsertRoomArchive(List<RoomArchive> roomArchiveList) {
        if (ObjectUtil.isEmpty(roomArchiveList)) {
            return 0;
        }
        return roomArchiveMapper.batchInsertRoomArchive(roomArchiveList);
    }
}
